<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="110px"
    >
      <el-form-item label="客户名称" prop="customName">
        <el-input
          v-model="queryParams.customName"
          placeholder="请输入客户名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="产品编号" prop="productNumber">
        <el-input
          v-model="queryParams.productNumber"
          placeholder="请输入产品编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="售后类型" prop="dictPostType">
        <el-select
          v-model="queryParams.dictPostType"
          placeholder="请选择售后类型"
          clearable
          class="!w-220px"
        >
          <el-option
            v-for="dict in getIntDictOptions('post_service_type')"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="时间选择" prop="queryTime">
        <el-date-picker
          v-model="queryParams.queryTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery" type="primary">搜索</el-button>
        <el-button @click="resetQuery"> 重置</el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <div style="width: 100%; height: 50px; display: flex; justify-content: space-between">
      <div>
        <el-button
          type="primary"
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['reportQuery:qualityReport:afterSalesMaintenanceCosts:export']"
        >
          数据导出
        </el-button>
      </div>
      <el-button type="primary" @click="openTableTitleSet"> 表头设置 </el-button>
    </div>
    <el-table
      ref="multipleTableRef"
      border
      row-key="uniqueId"
      :height="tableHeight"
      :span-method="objectSpanMethod"
      v-loading="loading"
      :data="list"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        align="center"
        width="60"
        :reserve-selection="true"
        type="selection"
        fixed="left"
      />
      <el-table-column
        v-for="(item, index) in tableTitleList"
        :key="index"
        :type="item.type"
        :min-width="item.width"
        :header-align="item.headerAlign"
        :align="item.align"
        :label="item.label"
        :prop="item.field"
      >
        <template #default="scope">
          <dict-tag v-if="item.dictType" :type="item.dictType" :value="scope.row[item.field]" />
          <template v-else>
            <template v-if="item.label == '序号'">{{ scope.row.cusIndex }}</template>
            <el-button
              v-else-if="item.label == '图纸图号'"
              type="primary"
              link
              @click="jumpPdfFile(scope.row[item.field])"
              >{{ scope.row[item.field] }}</el-button
            >

            <span v-else>{{ scope.row[item.field] }}</span>
          </template>
        </template>
      </el-table-column>
    </el-table>
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 导出 -->
  <ExportFile ref="exportRef" @download="downloadFun" />
  <!-- 表头设置 -->
  <TableWidthSet ref="tableTitleSet" @refresh="getList" />
</template>

<script setup lang="ts" name="CourseWelding">
   import { getIntDictOptions } from '@/utils/dict'
   import type { CrudSchema } from '@/hooks/web/useCrudSchemas'
   import download from '@/utils/download'
   import { jumpPdfFile } from '@/utils/formCreate'
   import * as afterSalesMaintenanceCosts from '@/api/reportQuery/qualityReport/afterSalesMaintenanceCosts'
   const loading = ref(true) // 列表的加载中
   const total = ref(0) // 列表的总页数
   const list = ref([]) // 列表的数据
   const queryParams = reactive({
     pageNo: 1,
     pageSize: 50,
     customName: null,
     productNumber: null,
     dictPostType: null,
     queryTime: null
   })
   const queryFormRef = ref() // 搜索的表单
   const multipleTableRef = ref()
   const exportLoading = ref(false) // 导出的加载中

   const tableTitleList = ref<CrudSchema[]>([]) // 表头列表
   //合并行
   const objectSpanMethod = ({rowIndex,column}) => {
     let spanArr = []
     let pos = ''
     const data = list.value
     const len = data.length
     for (let i = 0; i < len; i++) {
       if (i === 0) {
         spanArr.push(1)
         pos = 0
       } else {
         // 判断当前元素与上一个元素是否相同
         if (data[i].id === data[i - 1].id && !['staffName','serviceWorkTime', 'arriveSceneTime', 'leaveSceneTime'].includes(column.property)) {
          data[i].cusIndex = data[i - 1].cusIndex
           spanArr[pos] += 1
           spanArr.push(0)
         } else {
          data[i].cusIndex = data[i - 1].cusIndex + 1
           spanArr.push(1)
           pos = i
         }
       }
     }
     const _row = spanArr[rowIndex]
     const _col = _row > 0 ? 1 : 0
     return {
       rowspan: _row,
       colspan: _col
     }
   }
   /** 查询列表 */
   const getList = async () => {
     loading.value = true
     try {
       const data = await afterSalesMaintenanceCosts.getAfterSalesMaintenanceCostsPage(queryParams)
       data.list.map((item,index) => {
        item.cusIndex = ++index
       })
       list.value = data.list
       tableTitleList.value = data.labels
       total.value = data.total
     } finally {
       loading.value = false
     }
   }
   /**选中 */
   const selectList = ref([])
   const handleSelectionChange = (data) => {
     selectList.value = data
   }

   /** 搜索按钮操作 */
   const handleQuery = () => {
     queryParams.pageNo = 1
     getList()
   }

   /** 重置按钮操作 */
   const resetQuery = () => {
     queryFormRef.value.resetFields()
     handleQuery()
   }
   /** 导出按钮操作 */
   const exportRef = ref()
   const handleExport = () => {
     exportRef.value.open()
   }
   // 导出
   const downloadFun = async () => {
     const val = exportRef.value.params
     let params = {
       fileName: val.name,
       ids: ''
     }
     if (val.range === 'all') {
       params.ids = ''
     } else {
       params.ids = selectList.value.map((item) => item.id)
     }
     afterSalesMaintenanceCosts.exportAfterSalesMaintenanceCosts(params).then((res) => {
       download.excel(res, val.name)
       clearSelectData()
     })
   }
   //清空多选
   const clearSelectData = () => {
     selectList.value = []
     multipleTableRef.value!.clearSelection()
   }

   /**打开表格表头设置**/
   const tableTitleSet = ref()
   const openTableTitleSet = () => {
     let code = 'afterSalesMaintenanceCosts'
     tableTitleSet.value.open(tableTitleList.value, code)
   }

   /** 初始化 **/
   const tableHeight = ref(0)
   onMounted(() => {
     const queryParamsHeight = queryFormRef.value.$el.clientHeight + 2
     tableHeight.value = document.body.clientHeight - 132 - queryParamsHeight - 127
     getList()
   })
</script>
<style lang="scss" scoped>
   .fontHide {
     width: 100%;
     height: 100%;
     white-space: nowrap;
     text-overflow: clip;
     overflow: hidden;
   }
</style>